home *** CD-ROM | disk | FTP | other *** search
- Path: earth.superlink.net!usenet
- From: Michael Rizzo <rizzom@mars.superlink.net>
- Newsgroups: comp.lang.c++
- Subject: Strings, Templates--> confusion
- Date: Mon, 11 Mar 1996 23:22:42 -0500
- Organization: SuperNet Inc. (908) 828-8988
- Message-ID: <3144FC12.65B8@mars.superlink.net>
- NNTP-Posting-Host: ez5.superlink.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Hi all,
-
- Once again I am posting a novice question and hoping for your
- wisdom to help me. Here it goes:
- I have defined my own string class called string, as well as a
- 'kind of' array template class called MemManager. Then I have a third
- class called VMPROC that contains two instances of the MemManager
- template class one being a string instantiation and one being an
- integer instantion. The integer instantiation works just fine, but...
- the string instantion is driving my crazy. Any time I store a string to
- any one of the cells in the MemManager<string> array, it assigns that
- value to all of the cells. It seems like all of the cells are pointing
- to the same memory location or something like that. I think it's
- something in my string class, but I have not been able to figure out
- what it is. I have included the code (it is not in very good form at
- the moment). BTW, I have tried it with Visual C++ 4.0 compiling as a
- console app, and with DJGPP v2. I know this is alot to ask, but I am at
- wits end. I just can't figure out whats wrong. Thanks in advance to
- any who can spare some time to help. my e-mail is:
- rizzom@mars.superlink.net
-
- --Mike
-
- Source code listing:
-
- File string.h
- #ifndef IOSTREAM_H_
- #define IOSTREAM_H_
- #include <iostream.h>
- #endif
-
- #ifndef STDLIB_H_
- #define STDLIB_H_
- #include <stdlib.h>
- #endif
-
- //#include <new.h>
- #ifndef STRING_H_
- #define STRING_H_
- #include <string.h>
- #endif
-
-
- class string
- {
- int size;
- char *str;
- public:
- string (const string&);
- string (const char*);
- string (int);
- string();
- string (const char*, int);
- ~string();
- int GetLen() {return size;};
-
- char& operator[](int);
- int operator!() const;
- string& operator+=(const string&);
- string& operator=(char *);
- friend ostream& operator<<(ostream&, string&);
- };
-
-
- File string.cpp
- #ifndef MY_STRING_H_
- #define MY_STRING_H_
- #include "string.h"
- #endif
-
- string::string()
- {
- size = 0;
- str = new char[size + 1];
- str[0] = NULL;
- }
-
- string::string (const string& s)
- {
- size = s.size;
- str = new char[size + 1];
- strcpy(str, s.str);
- }
-
- string::string(const char *s)
- {
- size = strlen(s);
- str = new char[size+1];
- strcpy(str, s);
- }
-
-
- string::string(const char *cptr, int sz)
- {
- size = sz;
- str = new char[size +1];
- strcpy(str, cptr);
- }
-
- string::string(int sz)
- {
- size = sz;
- str = new char[size + 1];
-
- }
-
- string::~string()
- {
- delete str;
- //cout << "called ~string" << endl;
- }
-
-
- char& string::operator[](int ind)
- {
- if (ind >= 0 && ind < size)
- return str[ind];
- else
- {
- cout << "Subscript out of bounds" << endl;
- exit(-1);
- return str[ind];
- }
- }
-
- string& string::operator+=(const string& x1)
- {
- size += x1.size;
- char *tmp = new char[size+1];
- strcpy(tmp,str);
- strcat(tmp,x1.str);
- delete str;
- str = tmp;
- return *this;
- }
-
-
- int string::operator!() const
- {
- return size == 0;
- }
-
- string& string::operator=(char *s)
- {
- cout << "= invoked:: ";
- size = strlen(s);
- delete str;
- str = new char[size+1];
- strcpy(str,s);
- return *this;
- }
-
- //Friend function defs
- ostream& operator<<(ostream& os, string& tmp)
- {
- return os << tmp.str;
- }
-
- File memman.h
- #ifndef IOSTREAM_H_
- #define IOSTREAM_H_
- #include <iostream.h>
- #endif
-
- #ifndef STDLIB_H_
- #define STDLIB_H_
- #include <stdlib.h>
- #endif
-
- const int MemSize = 100;
-
- template <class T>
- class MemManager
- {
- void init(const T*, int);
-
- int size;
- T *mem;
-
- public:
- MemManager(int sz=MemSize) {mem = new T[size=sz];};
- MemManager(const T *m, int sz) {init(m,sz);};
- MemManager(const MemManager &M) {init (M.mem, M.size);};
- ~MemManager() {delete [] mem;};
-
- int GetSize() {return size;};
- T& Fetch(int ind) {return mem[ind];};
- void Store(T& item, int ind) {mem[ind] = item;};
- T& operator[] (int ind) {return mem[ind];};
- MemManager& operator=(const MemManager&);
- };
-
-
- File Memman.h
- #ifndef MEMMAN_H_
- #define MEMMAN_H_
- #include "MemMan.h"
- #endif
-
- template <class T>
- void MemManager<T>::init(const T *memman, int sz)
- {
- size = sz;
- mem = new T[size];
-
- for (int i=0; i<size;++i)
- mem[i] = memman[i];
- }
-
- template <class T>
- MemManager<T>& MemManager<T>::operator=(const MemManager &m)
- {
- if (this == &m) return *this;
- delete [] mem;
- init(m.mem, m.size);
- return *this;
- }
-
-
- File main.cpp
- #include "memman.h"
- #include "string.h"
-
- #ifndef IOSTREAM_H_
- #define IOSTREAM_H_
- #include <iostream.h>
- #endif
-
- #ifndef STDLIB_H_
- #define STDLIB_H_
- #include <stdlib.h>
- #endif
-
- #ifndef STDIO_H_
- #define STDIO_H_
- #include <stdio.h>
- #endif
-
- enum STATE {IDLE = 0, BUSY};
- enum boolean {FALSE=0, TRUE};
-
- class VMPROC
- {
- int AC, PC; //accum. program counters
- string IR; //instruction register
- STATE state; //machine state
- MemManager<int> dataMem; //date memory array
- MemManager<string> instMem; //instruction memory array
-
- public:
- VMPROC() {cout << "VMPROC instantiated" << endl;};
- ~VMPROC() {cout << "VMPROC destroyed" << endl;};
- STATE GetState() {return state;};
- boolean Init();
- void DisplayMem();
- };
-
- boolean VMPROC::Init()
- {
- string oneword(""), initstr("TEMP_INIT");
- char s[11], *f;
- int n= 0;
- strcpy(s, "TEMP2");
- for (int i=0;i<10;i++)
- {
- oneword = s;
- //oneword += onenum;
- if (i>4)
- {
- strcpy(s,"Last 5");
- n = i*100;
- }
- else
- {
- strcpy(s,"First 5");
- n = i*10;
- }
- instMem.Store(oneword, i);
- dataMem.Store(n, i);
-
- }
- cout << "FROM INIT:" << endl;
- cout << "intsMem: ";
- for (int j=0;j<10;j++)
- cout << instMem.Fetch(j) << " ";
- cout << endl;
- cout << "dataMem: ";
- for (int k=0;k<10;k++)
- cout << dataMem.Fetch(k) << " ";
- cout << endl;
- instMem.Store(initstr,5);
- /* the preceding line will not cause all cells in instMem to
- contain the value of initstr? I have no idea why*/
-
- return TRUE;
- }
-
-
- void VMPROC::DisplayMem()
- {
- cout << "\nFROM DISPLAYMEM: "<< endl;
- cout << "instMem: \n";
- for (int i=0;i<100;i++)
- cout << i << " : " << instMem.Fetch(i) << " ";
- cout << endl;
- cout << "dataMem: \n";
- for (int j=0;j<10;j++)
- cout << dataMem.Fetch(j) << " ";
- cout << endl;
- }
-
-
- int main()
- {
- VMPROC test;
- test.Init();
- test.DisplayMem();
- return 0;
- }
-
-
- OUTPUT:
-
- VMPROC instantiated
- FROM INIT:
- intsMem: Last 5 Last 5 Last 5 Last 5 Last 5 Last 5 Last 5 Last 5 Last 5
- Last 5
-
- *********THE OUPUT SHOULD BE
- First 5 First 5 First 5 First 5 First 5 Last 5 Last 5 Last 5 Last 5 Last
- 5
- *****************************
-
- dataMem: 0 10 20 30 40 500 600 700 800 900
-
- FROM DISPLAYMEM:
- instMem:
- 0 : Last 5 1 : Last 5 2 : Last 5 3 : Last 5 4 : Last 5 5 : TEMP_INIT 6 :
- Last 5 7 : Last 5 8 : Last 5 9 : Last 5 10 : 11 : 12 : 13 : 14 : 15
- : 16 : 17 : 18 : 19 : 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27
- : 28 : 29 : 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : 38 : 39
- : 40 : 41 : 42 : 43 : 44 : 45 : 46 : 47 : 48 : 49 : 50 : 51
- : 52 : 53 : 54 : 55 : 56 : 57 : 58 : 59 : 60 : 61 : 62 : 63
- : 64 : 65 : 66 : 67 : 68 : 69 : 70 : 71 : 72 : 73 : 74 : 75
- : 76 : 77 : 78 : 79 : 80 : 81 : 82 : 83 : 84 : 85 : 86 : 87
- : 88 : 89 : 90 : 91 : 92 : 93 : 94 : 95 : 96 : 97 : 98 : 99
- :
- dataMem:
- 0 10 20 30 40 500 600 700 800 900
- VMPROC destroyed
-
- If you got this far thanks again for your time.
-